import { ApiResponse, PaginatedResponse, ErrorResponse } from '@/common/interfaces/response.interface';
import { getCurrentContext } from '@/common/async-context';

export class ResponseUtil {
    /**
     * 成功响应
     */
    static success<T>(data: T, msg: string = 'success', code: number = 200): ApiResponse<T> {
        const context = getCurrentContext();
        return {
            code,
            msg,
            data,
            timestamp: new Date().toISOString(),
            traceId: context?.traceId || 'unknown',
        };
    }

    /**
     * 分页响应
     */
    static paginated<T>(data: T[], page: number, pageSize: number, total: number, msg: string = 'success', code: number = 200): PaginatedResponse<T> {
        const context = getCurrentContext();
        return {
            code,
            msg,
            data,
            timestamp: new Date().toISOString(),
            traceId: context?.traceId || 'unknown',
            pagination: {
                page,
                pageSize,
                total,
                totalPages: Math.ceil(total / pageSize),
            },
        };
    }

    /**
     * 错误响应
     */
    static error(msg: string = '操作失败', code: number = 500, error?: string, details?: any): ErrorResponse {
        const context = getCurrentContext();
        return {
            code,
            msg,
            data: null,
            timestamp: new Date().toISOString(),
            traceId: context?.traceId || 'unknown',
            error,
            details,
        };
    }

    /**
     * 自定义响应
     */
    static custom<T>(code: number, msg: string, data: T): ApiResponse<T> {
        const context = getCurrentContext();
        return {
            code,
            msg,
            data,
            timestamp: new Date().toISOString(),
            traceId: context?.traceId || 'unknown',
        };
    }
}
